//
//  MineDiscountCell.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/9/4.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "MineDiscountCell.h"

@interface MineDiscountCell ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *reminderLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property (nonatomic, strong) UILabel *priceLabel;
@end

@implementation MineDiscountCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        UIView *bgView = [UIView new];
        [self addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(20);
            make.right.equalTo(self).offset(-20);
            make.top.equalTo(self).offset(15);
            make.height.equalTo(@87.8);
        }];
        bgView.layer.shadowColor = ColorFrom16(0xDBDBDB).CGColor;
        bgView.layer.shadowRadius = 4;
        bgView.layer.shadowOpacity = 0.8;
        bgView.layer.shadowOffset = CGSizeMake(0, 0);
        bgView.layer.backgroundColor = ColorFrom16(0xffffff).CGColor;
        bgView.layer.cornerRadius = 2;
        
        _leftView = [UIView new];
        _leftView.backgroundColor = ColorFrom16(0xEE5959);
        [bgView addSubview:_leftView];
        [_leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bgView);
            make.left.equalTo(bgView);
            make.bottom.equalTo(bgView);
            make.width.equalTo(@2);
        }];
        
        _titleLabel = [UILabel new];
        _titleLabel.text = @"全场通用券";
        _titleLabel.textColor = ColorFrom16(0x505050);
        _titleLabel.font = PFRFONT(14);
        [bgView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bgView).offset(14);
            make.left.equalTo(_leftView.mas_right).offset(9.4);
            make.width.equalTo(@126);
            make.height.equalTo(@20);
        }];
        
        _reminderLabel = [UILabel new];
        _reminderLabel.text = @"满50元可用";
        _reminderLabel.textColor = ColorFrom16(0x333333);
        _reminderLabel.font = PFRFONT(14);
        _reminderLabel.attributedText = [GW_Tool setTextColor:ColorFrom16(0xEE5959) rangeOfString:@"50" string:_reminderLabel.text textFont:18];
        [bgView addSubview:_reminderLabel];
        [_reminderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.mas_bottom).offset(8);
            make.left.equalTo(_titleLabel);
            make.width.equalTo(@158);
            make.height.equalTo(@25);
        }];
        
        _dateLabel = [UILabel new];
        _dateLabel.text = @"有效期:17/07/10 23:02-17/08/17 23:02";
        _dateLabel.textColor = ColorFrom16(0xa4a4a4);
        _dateLabel.font = PFRFONT(10);
        [bgView addSubview:_dateLabel];
        [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_reminderLabel.mas_bottom).offset(2);
            make.left.equalTo(_reminderLabel);
            make.width.equalTo(@199);
            make.height.equalTo(@14);
        }];
        
        _priceLabel = [UILabel new];
        _priceLabel.text = @"5";
        _priceLabel.textAlignment = 1;
        _priceLabel.textColor = ColorFrom16(0xFFF6E2);
        _priceLabel.font = PFRFONT(32);
        _priceLabel.backgroundColor = ColorFrom16(0xEE5959);
        [bgView addSubview:_priceLabel];
        [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.equalTo(bgView).offset(7);
            make.right.equalTo(bgView).offset(-8);
            make.width.equalTo(@90.3);
            make.height.equalTo(@73.7);
        }];
        
        UIImageView *image = [UIImageView new];
        image.image = IMAGE(@"discount_money");
        [_priceLabel addSubview:image];
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_priceLabel).offset(9);
            make.left.equalTo(_priceLabel).offset(9);
            make.width.equalTo(@12);
            make.height.equalTo(@12);
        }];
        
        //虚线
        _shapeLayer = [CAShapeLayer layer];
        [_shapeLayer setBounds:bgView.bounds];
        [_shapeLayer setPosition:bgView.center];
        
        // 设置虚线颜色为blackColor
        [_shapeLayer setStrokeColor:[ColorFrom16(0xFFE898) CGColor]];
        
        // 3.0f设置虚线的高度
        [_shapeLayer setLineWidth:1.0f];
        [_shapeLayer setLineJoin:kCALineJoinRound];
        
        
        // 3=线的宽度 1=每条线的间距
        [_shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:6], [NSNumber numberWithInt:3], nil]];
        
        // 创建一个Path句柄
        CGMutablePathRef path = CGPathCreateMutable();
        
        // 初始化该path到一个初始点 0表示 x  100表示y
        CGPathMoveToPoint(path, NULL, GWIDTH - 146, 7);
        
        // 添加一条直线，从初始点到该函数指定的终点
        CGPathAddLineToPoint(path, NULL, GWIDTH - 146, 81);
        
        [_shapeLayer setPath:path];
        CGPathRelease(path);
        
        [bgView.layer addSublayer:_shapeLayer];
        
        _useImage = [UIImageView new];
        _useImage.image = IMAGE(@"mine_used");
        _useImage.hidden = YES;
        [bgView addSubview:_useImage];
        [_useImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bgView).offset(10);
            make.right.equalTo(bgView).offset(182);
            make.width.equalTo(@37);
            make.height.equalTo(@37);
        }];
        
    }
    return self;
}

- (void)configViewWithModel:(MindDiscountModel *)model
{
    float price = [model.full_price floatValue];
    _titleLabel.text = model.title;
    
    
    _reminderLabel.text = [NSString stringWithFormat:@"满%.f元可用", price];
    _dateLabel.text = [NSString stringWithFormat:@"有效期:%@-%@", model.start_time, model.end_time];
    NSString *str = [NSString stringWithFormat:@"%.f", price];
    if([[NSString stringWithFormat:@"%@", model.type] isEqualToString:@"1"])
    {
         _reminderLabel.attributedText = [GW_Tool setTextColor:ColorFrom16(0xFFC33E) rangeOfString:str string:_reminderLabel.text textFont:18];
        _priceLabel.backgroundColor = ColorFrom16(0xFFC33E);
        _leftView.backgroundColor = ColorFrom16(0xFFC33E);
        _shapeLayer.strokeColor = ColorFrom16(0xFFC33E).CGColor;
    }
    else
    {
        //5B70D8
        _reminderLabel.attributedText = [GW_Tool setTextColor:ColorFrom16(0x5B70D8) rangeOfString:str string:_reminderLabel.text textFont:18];
        _priceLabel.backgroundColor = ColorFrom16(0x5B70D8);
        _leftView.backgroundColor = ColorFrom16(0x5B70D8);
        _shapeLayer.strokeColor = ColorFrom16(0x5B70D8).CGColor;

    }
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
